/**
* @file        visual_tools
* @brief       
* @version     1.0.0
* @author      Changlin.Jing 
* @date        2023/7/31 11:41
* @copyright   Copyright  2022 Langge Software Co., Ltd. All rights reserved.
*/
#include "visual_tools.h"
#include "osapi.h"

BEGIN_SPACE_LANGE_VDR
END_SPACE_LANGE_VDR

bool vdr::VisualTools::ParseRawPoints2File(const char *fileName,
                                           const std::vector<std::shared_ptr<LOC_POS_SIGNAL>> &points) {
    geos::io::GeoJSONWriter geojsonwriter;
    geos::geom::PrecisionModel pm = geos::geom::PrecisionModel(geos::geom::PrecisionModel::FLOATING);
    GeometryFactory::Ptr factory_ = GeometryFactory::create(&pm, 0);
    int index = 1;
    std::vector<geos::io::GeoJSONFeature> features;
    for (auto point : points) {
        Coordinate coor = Coordinate{ point->mLongitude, point->mLatitude };
        Geometry*  pointPtr  = factory_->createPoint(coor);
        std::unique_ptr<Geometry> uniqGeoPtr = pointPtr->clone();
        geos::io::GeoJSONFeature feature{ std::move(uniqGeoPtr), std::map<std::string, geos::io::GeoJSONValue> {
                {"idx",index*1.0},
                {"height",point->mHeight*1.0},
                {"roll",point->mRoll*1.0},
                {"pitch",point->mPitch*1.0},
                {"yaw",point->mHeading*1.0},
                {"timestamp", point->mLocalTickTime * 1.0}
        } };
        features.push_back(feature);
        index++;
    }

    geos::io::GeoJSONFeatureCollection featureCollection(features);
    std::string geostr = geojsonwriter.write(featureCollection);
    return Write2File(fileName, geostr.c_str(), (int)geostr.size());
}

bool vdr::VisualTools::Write2File(const char *fileName, const char *content, uint32_t dataSize) {
    void* pFile = osFopen(fileName,"w");
    if (pFile == NULL)
    {
        return false;
    }
    osFseek(pFile, 0, SEEK_SET);
    osFwrite(content, (int)dataSize, 1, pFile);
    osFclose(pFile);
    return true;
}
